/**
 * @Title: BaseActivity.java
 * @Project DCB
 * @Package com.pdw.dcb.ui.activity
 * @Description: Activity基类
 * @author zeng.ww
 * @date 2012-11-23 下午03:43:49
 * @version V1.0
 */
package com.netstar.ui;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import com.netstar.util.PackageUtil;
import com.netstar.util.StringUtil;

/**
 * Activity基类
 * 
 * @author zou.sq
 * @since 2013-04-13 下午03:43:49
 * @version 1.0
 */
public class BaseActivity extends Activity {
	public static final int TOAST_STYLE_MSG = 1;
	public static final int TOAST_STYLE_CODE = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 默认的toast方法，该方法封装下面的两点特性：<br>
	 * 1、只有当前activity所属应用处于顶层时，才会弹出toast；<br>
	 * 2、默认弹出时间为 Toast.LENGTH_SHORT;
	 * 
	 * @param msg
	 *            弹出的信息内容
	 */
	protected void toast(final String msg) {
		if (this != null && PackageUtil.isTopApplication(this)) {
			this.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if (!StringUtil.isNullOrEmpty(msg)) {
						Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
	}

	/**
	 * 默认的toast方法，该方法封装下面的两点特性：<br>
	 * 1、只有当前activity所属应用处于顶层时，才会弹出toast；<br>
	 * 2、默认弹出时间为 Toast.LENGTH_SHORT;
	 * 
	 * @param context
	 *            上下文
	 * @param param
	 *            弹出的信息内容
	 */
	public void toast(final Context context, final String param) {
		if (context != null && PackageUtil.isTopApplication(context) && !StringUtil.isNullOrEmpty(param)) {
			((Activity) context).runOnUiThread(new Runnable() {
				@Override
				public void run() {
					if (!StringUtil.isNullOrEmpty(param)) {
						Toast.makeText(context, param, Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
	}

	protected void set(String key, String value) {
		SharedPreferences.Editor ed = getSharedPreferences("set", Context.MODE_WORLD_WRITEABLE).edit();
		ed.putString(key, value);
		ed.commit();
	}

	protected String get(String key) {
		SharedPreferences ed = getSharedPreferences("set", Context.MODE_WORLD_READABLE);
		String ul = ed.getString(key, "");
		return ul;
	}
}
